//
//  BaseShareTool.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/8.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "BaseShareTool.h"
#import <UMSocialCore/UMSocialCore.h>
#import "BaseShareView.h"
#import "MBProgressHUD+MJ.h"

@interface BaseShareTool()

@property (nonatomic,strong) BaseShareView *shareView;
@property (nonatomic,strong) UIImage *img;//分享的图片

@end

@implementation BaseShareTool

static BaseShareTool *_tool;

+ (instancetype)shareTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_tool) {
            _tool = [[BaseShareTool alloc]init];
        }
    });
    return _tool;
}

+ (BaseShareTool *)baseShareWithImg:(UIImage *)img
{
    BaseShareTool *tool = [BaseShareTool shareTool];
    tool.img = img;
    [tool shareDetail];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx637ea06a1c6d1fff" appSecret:@"1b2n3v4fx5v56vj8f3hd5t7g5f6hj8fh" redirectURL:@"https://www.baidu.com"];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105821097" appSecret:nil redirectURL:@"https://www.baidu.com"];
    return tool;
}

- (void)shareDetail
{
    BaseShareView *shareView = [[BaseShareView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.shareView = shareView;
    [shareView showshareView:^(ShareType type) {
        [self showShareViewClickType:type];
    }];
}
- (void)showShareViewClickType:(ShareType)shareType
{
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx637ea06a1c6d1fff" appSecret:@"1b2n3v4fx5v56vj8f3hd5t7g5f6hj8fh" redirectURL:nil];
    
    //如何修改toast“分享成功”&弹框的位置
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    [shareObject setShareImage:self.img];
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UMSocialPlatformType type = -2;
    shareType = (ShareType)shareType;
    if (shareType == shareTypeWX) {
        type = UMSocialPlatformType_WechatSession;
    }else if(shareType == shareTypeFriends){
        type = UMSocialPlatformType_WechatTimeLine;
    }else {//保存图片到相册
        [self saveImg:self.img];
        return;
    }
    
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:vc completion:^(id result, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showError:@"分享取消"];
            });
        }else{
            if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = result;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD showSuccess:@"分享成功"];
                });
            }else{
                UMSocialLogInfo(@"response data is %@",result);
            }
        }
    }];
}

//保存图片到相册
- (void)saveImg:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [MBProgressHUD showOnlyText:@"保存图片成功,请去相册查看" view:nil];
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}

@end
